package modalities;

import java.util.Observer;

import javax.speech.recognition.*;

/**
 * Prints audio events to System.out
 */
public class AudioLevelListener extends RecognizerAudioAdapter {

	boolean level;
	boolean activated;
	Double activationThreshold = 0.4;

	public boolean isActivated() {return activated;}
	public void setActivated(boolean activated) {this.activated = activated;}


	ObservableThing ob = new ObservableThing();


	public AudioLevelListener(Observer observer) {

		//If activiated at the beginning, produce incoherent exceptions
		this.activated = false;
		this.level = true;
		this.ob.addExternalObserver(observer);

	}

	public AudioLevelListener(boolean showLevel) {
		level = showLevel;
	}

	@Override
	public void speechStarted(RecognizerAudioEvent e) {
	}

	@Override
	public void speechStopped(RecognizerAudioEvent e) {
	}

	@Override
	public void audioLevel(RecognizerAudioEvent e) {

		if(activated)
		{
			//Give a value between 0..1 ->
			Double level = 0.0;

			if (e != null)
			{
				level =(double) e.getAudioLevel();	
			}
			if(level > activationThreshold)
			{
				ob.externalSetChanged();
				ob.notifyObservers(new Message(level,true));
			}
		}
	}

}
